/*********************************************************************************************
* 文件：main.c
* 作者：Zhouchj 2020.09.23
* 描述：用户main函数源文件
* 修改：
* 注释：
*********************************************************************************************/
#include "rtthread.h"
#include "cm_backtrace.h"
#include "stm32f4xx_hal.h"
#include "drv_rtt_led/drv_rtt_led.h"
#include "drv_rtt_key/drv_rtt_key.h"
#include "drv_hal_led/drv_hal_led.h"
#include "drv_hal_key/drv_hal_key.h"

//#define DBG_LVL           DBG_INFO
#define DBG_LVL           DBG_LOG
//#define DBG_LVL           DBG_NODBG
#include <mydbg.h>          // must after of DBG_LVL, DBG_TAG or other options

#define HARDWARE_VERSION               "V1.0.0"

__align(32) rt_uint8_t mem2base[1024] __attribute__((at(0X10000000)));  //使用内部CCM内存池

void fault_test_by_unalign(void) 
{
    volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
    volatile int * p;
    volatile int value;

    *SCB_CCR |= (1 << 3); /* bit3: UNALIGN_TRP. */

    p = (int *) 0x00;
    value = *p;
    rt_kprintf("addr:0x%02X value:0x%08X\r\n", (int) p, value);

    p = (int *) 0x04;
    value = *p;
    rt_kprintf("addr:0x%02X value:0x%08X\r\n", (int) p, value);

    p = (int *) 0x03;
    value = *p;
    rt_kprintf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
}

void fault_test_by_div0(void) 
{
    volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
    int x, y, z;

    *SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */

    x = 10;
    y = 0;
    z = x / y;
    rt_kprintf ("z:%d\n", z);
}

int main(void)
{
#ifdef BOARD_CONFIG_USING_CMBACKTRACE
  cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, CMB_SW_VERSION);
#endif
  led_pin_init();
  key_pin_init();
  int value = 0;
  
  led_ctrl(0xff);
  rt_thread_mdelay(200);
  led_ctrl(0x00);
  while(1){
    u8 value = KEY_Scan(0);
    if(value){
      logfn_i("value:%d",value);
    }
    rt_thread_mdelay(10);
    
//    logfn_i("SysClock:%d PCLK1:%d PCLK2:%d value:%d",HAL_RCC_GetHCLKFreq(),HAL_RCC_GetPCLK1Freq(),HAL_RCC_GetPCLK2Freq(),value);
//    rt_thread_mdelay(1000);
  }
  return 0;
}
